<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of demgmm2</title>
  <meta name="keywords" content="demgmm2">
  <meta name="description" content="DEMGMM1 Demonstrate density modelling with a Gaussian mixture model.">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../../menu.html">Home</a> &gt;  <a href="#">ReBEL-0.2.7</a> &gt; <a href="#">netlab</a> &gt; demgmm2.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../../menu.html"><img alt="<" border="0" src="../../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="menu.html">Index for .\ReBEL-0.2.7\netlab&nbsp;<img alt=">" border="0" src="../../right.png"></a></td></tr></table>-->

<h1>demgmm2
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>DEMGMM1 Demonstrate density modelling with a Gaussian mixture model.</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>This is a script file. </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment">DEMGMM1 Demonstrate density modelling with a Gaussian mixture model.

    Description
    The problem consists of modelling data generated by a mixture of
    three Gaussians in 2 dimensions.  The priors are 0.3, 0.5 and 0.2;
    the centres are (2, 3.5), (0, 0) and (0,2); the variances are 0.2,
    0.5 and 1.0. The first figure contains a  scatter plot of the data.

    A Gaussian mixture model with three components is trained using EM.
    The parameter vector is printed before training and after training.
    The user should press any key to continue at these points.  The
    parameter vector consists of priors (the column), centres (given as
    (x, y) pairs as the next two columns), and variances (the last
    column).

    The second figure is a 3 dimensional view of the density function,
    while the third shows the 1-standard deviation circles for the three
    components of the mixture model.

    See also
    <a href="gmm.html" class="code" title="function mix = gmm(dim, ncentres, covar_type, ppca_dim)">GMM</a>, <a href="gmminit.html" class="code" title="function mix = gmminit(mix, x, options)">GMMINIT</a>, <a href="gmmem.html" class="code" title="function [mix, options, errlog] = gmmem(mix, x, options)">GMMEM</a>, <a href="gmmprob.html" class="code" title="function prob = gmmprob(mix, x)">GMMPROB</a>, <a href="gmmunpak.html" class="code" title="function mix = gmmunpak(mix, p)">GMMUNPAK</a></pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="dem2ddat.html" class="code" title="function [data, c, prior, sd] = dem2ddat(ndata)">dem2ddat</a>	DEM2DDAT Generates two dimensional data for demos.</li><li><a href="gmm.html" class="code" title="function mix = gmm(dim, ncentres, covar_type, ppca_dim)">gmm</a>	GMM	Creates a Gaussian mixture model with specified architecture.</li><li><a href="gmmem.html" class="code" title="function [mix, options, errlog] = gmmem(mix, x, options)">gmmem</a>	GMMEM	EM algorithm for Gaussian mixture model.</li><li><a href="gmminit.html" class="code" title="function mix = gmminit(mix, x, options)">gmminit</a>	GMMINIT Initialises Gaussian mixture model from data</li><li><a href="gmmprob.html" class="code" title="function prob = gmmprob(mix, x)">gmmprob</a>	GMMPROB Computes the data probability for a Gaussian mixture model.</li></ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="demnlab.html" class="code" title="function demnlab(action);">demnlab</a>	DEMNLAB A front-end Graphical User Interface to the demos</li></ul>
<!-- crossreference -->


<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <span class="comment">%DEMGMM1 Demonstrate density modelling with a Gaussian mixture model.</span>
0002 <span class="comment">%</span>
0003 <span class="comment">%    Description</span>
0004 <span class="comment">%    The problem consists of modelling data generated by a mixture of</span>
0005 <span class="comment">%    three Gaussians in 2 dimensions.  The priors are 0.3, 0.5 and 0.2;</span>
0006 <span class="comment">%    the centres are (2, 3.5), (0, 0) and (0,2); the variances are 0.2,</span>
0007 <span class="comment">%    0.5 and 1.0. The first figure contains a  scatter plot of the data.</span>
0008 <span class="comment">%</span>
0009 <span class="comment">%    A Gaussian mixture model with three components is trained using EM.</span>
0010 <span class="comment">%    The parameter vector is printed before training and after training.</span>
0011 <span class="comment">%    The user should press any key to continue at these points.  The</span>
0012 <span class="comment">%    parameter vector consists of priors (the column), centres (given as</span>
0013 <span class="comment">%    (x, y) pairs as the next two columns), and variances (the last</span>
0014 <span class="comment">%    column).</span>
0015 <span class="comment">%</span>
0016 <span class="comment">%    The second figure is a 3 dimensional view of the density function,</span>
0017 <span class="comment">%    while the third shows the 1-standard deviation circles for the three</span>
0018 <span class="comment">%    components of the mixture model.</span>
0019 <span class="comment">%</span>
0020 <span class="comment">%    See also</span>
0021 <span class="comment">%    GMM, GMMINIT, GMMEM, GMMPROB, GMMUNPAK</span>
0022 <span class="comment">%</span>
0023 
0024 <span class="comment">%    Copyright (c) Ian T Nabney (1996-2001)</span>
0025 
0026 <span class="comment">% Generate the data</span>
0027 <span class="comment">% Fix seeds for reproducible results</span>
0028 randn(<span class="string">'state'</span>, 42);
0029 rand(<span class="string">'state'</span>, 42);
0030 
0031 ndata = 500;
0032 [data, datac, datap, datasd] = <a href="dem2ddat.html" class="code" title="function [data, c, prior, sd] = dem2ddat(ndata)">dem2ddat</a>(ndata);
0033 
0034 clc
0035 disp(<span class="string">'This demonstration illustrates the use of a Gaussian mixture model'</span>)
0036 disp(<span class="string">'to approximate the unconditional probability density of data in'</span>)
0037 disp(<span class="string">'a two-dimensional space.  We begin by generating the data from'</span>)
0038 disp(<span class="string">'a mixture of three Gaussians and plotting it.'</span>)
0039 disp(<span class="string">' '</span>)
0040 disp(<span class="string">'Press any key to continue'</span>)
0041 pause
0042 
0043 fh1 = figure;
0044 plot(data(:, 1), data(:, 2), <span class="string">'o'</span>)
0045 set(gca, <span class="string">'Box'</span>, <span class="string">'on'</span>)
0046 <span class="comment">% Set up mixture model</span>
0047 ncentres = 3;
0048 input_dim = 2;
0049 mix = <a href="gmm.html" class="code" title="function mix = gmm(dim, ncentres, covar_type, ppca_dim)">gmm</a>(input_dim, ncentres, <span class="string">'spherical'</span>);
0050 
0051 options = foptions;
0052 options(14) = 5;    <span class="comment">% Just use 5 iterations of k-means in initialisation</span>
0053 <span class="comment">% Initialise the model parameters from the data</span>
0054 mix = <a href="gmminit.html" class="code" title="function mix = gmminit(mix, x, options)">gmminit</a>(mix, data, options);
0055 
0056 clc
0057 disp(<span class="string">'The data is drawn from a mixture with parameters'</span>)
0058 disp(<span class="string">'    Priors        Centres         Variances'</span>)
0059 disp([datap' datac (datasd.^2)'])
0060 disp(<span class="string">' '</span>)
0061 disp(<span class="string">'The mixture model has three components and spherical covariance'</span>)
0062 disp(<span class="string">'matrices.  The model parameters after initialisation using the'</span>)
0063 disp(<span class="string">'k-means algorithm are as follows'</span>)
0064 <span class="comment">% Print out model</span>
0065 disp(<span class="string">'    Priors        Centres         Variances'</span>)
0066 disp([mix.priors' mix.centres mix.covars'])
0067 disp(<span class="string">'Press any key to continue'</span>)
0068 pause
0069 
0070 <span class="comment">% Set up vector of options for EM trainer</span>
0071 options = zeros(1, 18);
0072 options(1)  = 1;        <span class="comment">% Prints out error values.</span>
0073 options(14) = 10;        <span class="comment">% Max. Number of iterations.</span>
0074 
0075 disp(<span class="string">'We now train the model using the EM algorithm for 10 iterations'</span>)
0076 disp(<span class="string">' '</span>)
0077 disp(<span class="string">'Press any key to continue'</span>)
0078 pause
0079 [mix, options, errlog] = <a href="gmmem.html" class="code" title="function [mix, options, errlog] = gmmem(mix, x, options)">gmmem</a>(mix, data, options);
0080 
0081 <span class="comment">% Print out model</span>
0082 disp(<span class="string">' '</span>)
0083 disp(<span class="string">'The trained model has parameters '</span>)
0084 disp(<span class="string">'    Priors        Centres         Variances'</span>)
0085 disp([mix.priors' mix.centres mix.covars'])
0086 disp(<span class="string">'Note the close correspondence between these parameters and those'</span>)
0087 disp(<span class="string">'of the distribution used to generate the data, which are repeated here.'</span>)
0088 disp(<span class="string">'    Priors        Centres         Variances'</span>)
0089 disp([datap' datac (datasd.^2)'])
0090 disp(<span class="string">' '</span>)
0091 disp(<span class="string">'Press any key to continue'</span>)
0092 pause
0093 
0094 clc
0095 disp(<span class="string">'We now plot the density given by the mixture model as a surface plot'</span>)
0096 disp(<span class="string">' '</span>)
0097 disp(<span class="string">'Press any key to continue'</span>)
0098 pause
0099 <span class="comment">% Plot the result</span>
0100 x = -4.0:0.2:5.0;
0101 y = -4.0:0.2:5.0;
0102 [X, Y] = meshgrid(x,y);
0103 X = X(:);
0104 Y = Y(:);
0105 grid = [X Y];
0106 Z = <a href="gmmprob.html" class="code" title="function prob = gmmprob(mix, x)">gmmprob</a>(mix, grid);
0107 Z = reshape(Z, length(x), length(y));
0108 c = mesh(x, y, Z);
0109 hold on
0110 title(<span class="string">'Surface plot of probability density'</span>)
0111 hold off
0112 
0113 clc
0114 disp(<span class="string">'The final plot shows the centres and widths, given by one standard'</span>)
0115 disp(<span class="string">'deviation, of the three components of the mixture model.'</span>)
0116 disp(<span class="string">' '</span>)
0117 disp(<span class="string">'Press any key to continue.'</span>)
0118 pause
0119 <span class="comment">% Try to calculate a sensible position for the second figure, below the first</span>
0120 fig1_pos = get(fh1, <span class="string">'Position'</span>);
0121 fig2_pos = fig1_pos;
0122 fig2_pos(2) = fig2_pos(2) - fig1_pos(4);
0123 fh2 = figure;
0124 set(fh2, <span class="string">'Position'</span>, fig2_pos)
0125 
0126 hp1 = plot(data(:, 1), data(:, 2), <span class="string">'bo'</span>);
0127 axis(<span class="string">'equal'</span>);
0128 hold on
0129 hp2 = plot(mix.centres(:, 1), mix.centres(:,2), <span class="string">'g+'</span>);
0130 set(hp2, <span class="string">'MarkerSize'</span>, 10);
0131 set(hp2, <span class="string">'LineWidth'</span>, 3);
0132 
0133 title(<span class="string">'Plot of data and mixture centres'</span>)
0134 angles = 0:pi/30:2*pi;
0135 <span class="keyword">for</span> i = 1 : mix.ncentres
0136   x_circle = mix.centres(i,1)*ones(1, length(angles)) + <span class="keyword">...</span>
0137     sqrt(mix.covars(i))*cos(angles);
0138   y_circle = mix.centres(i,2)*ones(1, length(angles)) + <span class="keyword">...</span>
0139     sqrt(mix.covars(i))*sin(angles);
0140   plot(x_circle, y_circle, <span class="string">'r'</span>)
0141 <span class="keyword">end</span>
0142 hold off
0143 disp(<span class="string">'Note how the data cluster positions and widths are captured by'</span>)
0144 disp(<span class="string">'the mixture model.'</span>)
0145 disp(<span class="string">' '</span>)
0146 disp(<span class="string">'Press any key to end.'</span>)
0147 pause
0148 
0149 close(fh1);
0150 close(fh2);
0151 clear all; 
0152</pre></div>
<hr><address>Generated on Tue 26-Sep-2006 10:36:21 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>